package com.seal.commons.copiers.bean;


import com.seal.commons.copiers.Copiers;
import com.seal.commons.copiers.base.Copier;
import com.seal.commons.copiers.exception.CopierException;

import java.io.Serializable;

/**
 * 功能描述: <br/>
 *
 * @author huangyong
 * @date: 2020-06-01 21:21<br/>
 * @since JDK 1.8
 */
public class BOSupport<ID extends Serializable> implements Serializable {

    private static final long serialVersionUID = -8494325806532606541L;


    private ID id;

    public ID getId() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    /**
     * 目标对象转化
     * @param object
     */
    public void parse(Object object){
        try{
            Copier copier = Copiers.createOrika(object.getClass(), this.getClass()).register();
            copier.copy(object, this);
        }catch(Exception e){
            throw new CopierException(e);
        }
    }

    /**
     * 对象拷贝，仅支持对象字段名一致的进行拷贝，如果字段名不一致，请参考copier.createOrika的增强处理
     * @param clazz
     * @param <E>
     * @return
     */
    public <E> E toEntity(Class<E> clazz) {
        try{
            Copier copier = Copiers.createOrika(this.getClass(), clazz).register();
            return (E)copier.copy(this);
        }catch(Exception e){
            throw new CopierException(e);
        }
    }

}